package org.budo.filestore.aliyun.oss;

import org.budo.filestore.impl.AbstractFileStore;

import com.aliyun.oss.OSSClient;

import lombok.Getter;
import lombok.Setter;

/**
 * @author lmw
 */
@Getter
@Setter
public abstract class AbstractAliyunOssFileStore extends AbstractFileStore {
    private String endpoint;

    private String accessKeyId;

    private String secretAccessKey;

    private String bucketName;

    private OSSClient ossClient = null;

    protected OSSClient getOssClient() {
        if (null != this.ossClient) {
            return this.ossClient;
        }

        synchronized (AbstractAliyunOssFileStore.class) {
            if (null != this.ossClient) {
                return this.ossClient;
            }

            return this.ossClient = this.initOssClient();
        }
    }

    private OSSClient initOssClient() {
        String endpoint = this.getEndpoint();
        if (null == endpoint || endpoint.isEmpty()) {
            throw new IllegalArgumentException("#43 endpoint=" + endpoint + ", this=" + this);
        }

        String accessKeyId = this.getAccessKeyId();
        if (null == accessKeyId || accessKeyId.isEmpty()) {
            throw new IllegalArgumentException("#48 accessKeyId=" + accessKeyId + ", this=" + this);
        }

        String secretAccessKey = this.getSecretAccessKey();
        if (null == secretAccessKey || secretAccessKey.isEmpty()) {
            throw new IllegalArgumentException("#53 secretAccessKey=" + secretAccessKey + ", this=" + this);
        }

        return new OSSClient(endpoint, accessKeyId, secretAccessKey);
    }
}